package com.atguigu.educms.controller;


import com.atguigu.common.R;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台banner管理接口
 * </p>
 *
 * @author Panghl
 * @since 2021-01-31
 */
@RestController
@RequestMapping("/educms/banner/")
@Api(tags = {"轮播图管理"})
public class BannerAdminController {

    @Autowired
    private CrmBannerService  crmBannerService;

    @ApiOperation(value = "分页查询banner")
    @PostMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable("page")Long page,
                        @PathVariable("limit")Long limit,
                        @RequestBody CrmBanner crmBanner){
        PageListBean pageListBean = crmBannerService.pageBanner(page,limit,crmBanner);
        return R.ok().data(pageListBean);
    }

    @ApiOperation(value = "添加banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner banner){
        try {
            crmBannerService.save(banner);
            return R.ok();
        }catch (Exception e){
            e.getMessage();
            return R.error();
        }
    }

    @ApiOperation(value = "修改banner")
    @PostMapping("editBanner")
    public R editBanner(@RequestBody CrmBanner banner){
        try {
            crmBannerService.updateById(banner);
            return R.ok();
        }catch (Exception e){
            e.getMessage();
            return R.error();
        }
    }


    @ApiOperation(value = "删除banner")
    @DeleteMapping("removeBanner/{id}")
    public R removeBanner(@PathVariable("id") String id){
        try {
            crmBannerService.removeById(id);
            return R.ok();
        }catch (Exception e){
            e.getMessage();
            return R.error();
        }
    }

    @ApiOperation(value = "获取单个banner信息")
    @GetMapping("get/{id}")
    public R get(@PathVariable("id") String id){
        try {
            return R.ok().data("banner",crmBannerService.getById(id));
        }catch (Exception e){
            e.getMessage();
            return R.error();
        }
    }

}
